package com.jackrain.nea.fc.ps.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品档案服务接口mapper层
 *
 * @author wuwenxue
 * @date 2017/10/20
 **/
@Mapper
public interface ViewCproMapper {

    /**
     * 根据传入json条件查询主表记录
     *
     * @param
     * @return List
     */
    @SelectProvider(type = ViewCproSql.class, method = "findCpro")
    List<HashMap> findAllPro(@Param("id") Object[] id);

    class ViewCproSql {
        public String findCpro(Map<String, Object> map) {
            Object[] id = (Object[]) map.get("id");
            return new SQL() {
                {
                    SELECT("ECODE,CP_C_DISTRIB_ID,ENAME,PS_C_BRAND_ID,DATEMARKET,CP_C_SUPPLIER_ID,FACTORYCODE,FABDESC,TAGSPEC," +
                            "PRICECOSTLIST,PRICELIST,PRICESETTLE,PRICELOWER,MINLOTSIZE,UNIT,TRIALDAYS,DATEOFFSHELF,SALEPERIOD,RECYCLE," +
                            "RELIABILITY,CAPACITY,PURCHASEMODE,REMARK,ISACTIVE,OWNERID,CREATIONDATE,MODIFIERID,MODIFIEDDATE,PROYEAR," +
                            "PROSEA,SEX,LARGECLASS,SUPBRAND,PROSOURCE,PURNATURE,BUYER,PRONATURE,PROMOTIONTYPE,PROLINE,POPULAR,STYLE," +
                            "DETAILS,BUYPATTERNER,SHELFCODE,PRICEBAND,PROMONTH,PROBAND,FABRIC,SERIES,SAFETECHCLASS,PROSTANDARD,FOLLOWER," +
                            "NEWPROER,MDLARGECLASS,MDMIDDLECLASS,CP_C_WAREHOUSE_ID,DISCENTER,PROSTANDARD2,TRIALSTORENUM,DISSTORENUM," +
                            "REDISSTORENUM,COMPOSITION,NUMDIM1,DESIGNER,THEMESTORY,ID,GBCODE,SYNC_STOREPRICE,SYNC_SUPPRICE,TXTDIM2," +
                            "TXTDIM3,NUMDIM2,NUMDIM3,NUMDIM4,NUMDIM5,NUMDIM6,NUMDIM7,NUMDIM8,NUMDIM9,NUMDIM10,NUMDIM11,NUMDIM12," +
                            "NUMDIM13,NUMDIM14,NUMDIM15,NUMDIM16,NUMDIM17,NUMDIM18,NUMDIM19,NUMDIM20,NUMDIM21,NUMDIM22,NUMDIM23," +
                            "NUMDIM24,NUMDIM25,NUMDIM27,TXTDIM4,IMAGE,NUMDIM26,IMAGE_SKU"
                    );
                    FROM("PS_C_PRO");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < id.length; i++) {
                        sb.append(id[i] + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    WHERE("ID IN (" + sb.toString() + ")");
                    WHERE("CP_C_DISTRIB_ID>0");
                }
            }.toString();
        }
    }

    /**
     * 根据外键关联表PS_C_BRAND查询返回ECODE
     *
     * @param brandID
     * @return String
     */
    @Select("select ECODE from PS_C_BRAND where ID=#{brandID}")
    String findByBrandID(Long brandID);

    /**
     * 根据外键关联表PS_C_PRODIM_ITEM查询返回ECODE
     *
     * @param id
     * @return String
     */
    @Select("select ECODE from PS_C_PRODIM_ITEM where ID=#{id}")
    String findByID(Long id);

    /**
     * 外键关联配销中心表CP_C_STOREORG
     *
     * @param id
     * @return String
     */
    @Select("select ECODE from CP_C_STOREORG where ID=#{id}")
    String findByDistrib(Long id);

    /**
     * 外键关联供应商表CP_C_SUPPLIER
     *
     * @param id
     * @return String
     */
    @Select("select ECODE from CP_C_SUPPLIER where ID=#{id}")
    String findBySupplier(Long id);

    /**
     * 外键关联用户表USERS
     *
     * @param id
     * @return String
     */
    @Select("select ENAME from USERS where ID=#{id}")
    String findByUserId(Long id);

    /**
     * 外键关联仓库档案CP_C_STORE
     *
     * @param id
     * @return String
     */
    @Select("select ECODE from CP_C_STORE where ID=#{id}")
    String findByStore(Long id);

    @Select("SELECT ECODE FROM CP_C_SUPPLIER WHERE ID IN (SELECT CP_C_SUPPLIER_ID FROM PS_C_PRO_SUPITEM WHERE PS_C_PRO_ID=#{id})")
    List<String> selBySupplier(Long id);

}
